package com.macken.wow.controller;


import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.macken.wow.dao.UserDao;
import com.macken.wow.entity.UserInfo;
import com.macken.wow.util.SendMailThread;
import com.macken.wow.util.ServletUtil;
import com.macken.wow.util.SignatureUtil;

@Controller
public class RegController extends BaseController {
	UserDao userDao = UserDao.instance;


	@RequestMapping("/reg.do")
	public void reg(HttpServletRequest request, HttpServletResponse response, @RequestParam String name,
			@RequestParam String pass, @RequestParam String email) throws Exception {
		System.out.println("name:" + name);
		name = name.toUpperCase().trim();
		UserInfo user = userDao.getUserByName(name);
		if (user != null) {
			response.getWriter().write("<script>alert('用户名已存在');location.href='/reg.jsp';</script>");
			return;
		}
		email=email.trim();
		user = userDao.getUserByVerifyEmail(email);
		if (user != null) {
			response.getWriter().write("<script>alert('email已被注册');location.href='/reg.jsp';</script>");
			return;
		}

		user = new UserInfo();
		user.userName = name;
		user.userPwd = SignatureUtil.md5(pass);
		user.userEmail = email;
		user.createTime = System.currentTimeMillis();
		user.createIp = getIp(request);
		user.verifyCode = UUID.randomUUID().toString();
		boolean res = userDao.insert(user);
		if (res) {
			String userName = name;
			String userPass = SignatureUtil.sha1(userName + ":" + pass.toUpperCase()).toUpperCase();
			userDao.insertRealMd(userName, userPass, email);
			int wowId = userDao.getWowId(userName);
			if (wowId > 0) {
				userDao.updateUserWowId(wowId, user.getUserName());
			}
		}
		//		response.getWriter().write("注册成功！");
		SendMailThread thread = new SendMailThread(user.userEmail, user.verifyCode);
		thread.start();
		response.sendRedirect("/msg.jsp?msg=regsuccess");
		return;
	}

	@RequestMapping("/login.do")
	public void login(HttpServletRequest request, HttpServletResponse response, @RequestParam String name,
			@RequestParam String pass) throws Exception {
		Gson gson = new Gson();
		name = name.toUpperCase().trim();
		UserInfo user = userDao.getUserByName(name);
		String md5Pass = SignatureUtil.md5(pass.trim());
		if (md5Pass.equals(user.userPwd)) {
			ServletUtil.setSession(request, "user", user);
			response.getWriter().write(gson.toJson("success"));
			return;
		} else {
			response.getWriter().write(gson.toJson("fail"));
			return;
		}
	}

	@RequestMapping("/logout.do")
	public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ServletUtil.removeSession(request, "user");
		response.sendRedirect("/");
		return;
	}
	@RequestMapping("/verify.do")
	public void verify(HttpServletRequest request, HttpServletResponse response, @RequestParam String email,
			@RequestParam String code) throws Exception {
		System.out.println("email:" + email);
		UserInfo user = userDao.getUserByEmailCode(email, code);
		if (user != null) {
			user.emailVerify = 1;
			userDao.updateUserVerify(user);
			response.sendRedirect("/msg.jsp?msg=verify");
			return;
		}
		response.sendRedirect("/msg.jsp");
		return;
	}
	@RequestMapping("/checkname.do")
	public void checkName(HttpServletRequest request, HttpServletResponse response, @RequestParam String name)
			throws Exception {
		Gson gson = new Gson();
		name = name.toUpperCase().trim();
		UserInfo user = userDao.getUserByName(name);
		if (user != null) {
			response.getWriter().write(gson.toJson("500"));
			return;
		} else {
			response.getWriter().write(gson.toJson("200"));
			return;
		}
	}



}
